<script setup lang="ts">

import {useRoute, useRouter} from "vue-router";
import {useStore} from "vuex";
import {onMounted, ref, watch} from "vue";

const store = useStore();
const router = useRouter();
const route = useRoute();

const navbarType = ref(store.state.navbarType);
const navbarTitle = ref(store.state.navbarTitle);

const goBack = () => {
  if(route.name == 'bindCardExcess'){
    router.go(-2);
  } else {
    router.go(-1);
  }
};

const changeLang = () => {
  store.state.langState = true;
}

const dynamicWatch = (params: string[]) => {
  if (params.includes('navbarType')) {
    watch(() => store.state.navbarType, (newNavbarType: boolean) => {
      navbarType.value = newNavbarType;
    });
  }
  if (params.includes('navbarTitle')) {
    watch(() => store.state.navbarTitle, (newNavbarTitle: boolean) => {
      navbarTitle.value = newNavbarTitle;
    });
  }
};

onMounted(() => {
  dynamicWatch(['navbarType', 'navbarTitle']);
});

</script>

<template>
  <van-sticky>
    <div v-if="navbarType == 'childPage'">
      <div class="navbar">
        <div>
          <van-icon name="arrow-left" size="28" @click="goBack()"/>
        </div>
        <div class="navbar-title">
          <span>{{ navbarTitle }}</span>
        </div>
      </div>
    </div>

    <div v-else-if="navbarType == 'firstPage'">
      <div class="navbar">
        <div class="navbar-title">
          <span>{{ navbarTitle }}</span>
        </div>
      </div>
    </div>

    <div v-else>
      <div class="navbar">
        <div class="navbar-title">
          <span>{{ navbarTitle }}</span>
        </div>
      </div>
    </div>
  </van-sticky>
</template>

<style lang="less" scoped>
.navbar {
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #000000;
  font-weight: bold;
  padding: 0 10px;
  font-size: 20px;
  background-color: #ffffff;

  .navbar-title {
    flex-grow: 1;
    text-align: center;
  }
}
</style>